package cn.maidouya.user.controller;

import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.user.domain.Logininfo;
import cn.maidouya.user.query.LogininfoQuery;
import cn.maidouya.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "中间表接口",description = "中间表接口详细描述")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找中间表",notes = "根据ID查找中间表")
    public Logininfo loadById(@PathVariable Long id) {
        Logininfo one = logininfoService.loadById(id);
        return one;
    }
    @GetMapping
    @ApiOperation(value = "查找全部中间表",notes = "查找所有中间表信息")
    public List<Logininfo> loadAll() {
        List<Logininfo> list = logininfoService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找中间表分页数据",notes = "分页查找所有中间表信息")
    public PageList<Logininfo> queryData(@RequestBody LogininfoQuery logininfoQuery) {
        PageList<Logininfo> pageList = logininfoService.queryData(logininfoQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "添加和修改中间表",notes = "添加和修改中间表")
    public AjaxResult saveAndUpdate(@RequestBody Logininfo logininfo) {
                try {
                    if (logininfo.getId() == null){
                        logininfoService.save(logininfo);
                    } else {
                        logininfoService.update(logininfo);
                    }
                    return new AjaxResult();
                } catch (Exception e) {
                    e.printStackTrace();
                    return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
                }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除中间表",notes = "根据id删除中间表")
    public AjaxResult delete(@PathVariable Long id) {
        try {
          logininfoService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }
}